package token;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;

public class TokenTimer {

    public void useTimer(int ID) throws ParseException {

        MyTimer mt = new MyTimer();
        mt.setID(ID);


        Timer timer = new Timer();

        Date dateNow = new Date();

        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String nowTimeStr = df.format(dateNow);
        Map<String, String> map = rsTime(nowTimeStr);
        System.out.println("计时器开始的时间为" + nowTimeStr);
        Integer year = Integer.parseInt(map.get("year"));
        Integer month = Integer.parseInt(map.get("month"));
        Integer day = Integer.parseInt(map.get("day"));
        Integer hour = Integer.parseInt(map.get("hour"));
        Integer minute = Integer.parseInt(map.get("minute"));
        Integer second = Integer.parseInt(map.get("second"));

        if ((minute += 60) >= 60) {
            minute -= 60;
            hour += 1;
            if (hour >= 24) {
                day += 1;
            }
        }

        String furtureTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second;
        Date dateFurture = df.parse(furtureTime);
        System.out.println("计时器预计完成的时间为" + furtureTime);
        timer.schedule(mt, dateFurture);
    }

    public static Map<String, String> rsTime(String time) {
        Map<String, String> map = new HashMap<>();
        char[] timeChars = time.toCharArray();
        int numY = time.indexOf("/");
        String year = time.substring(0, numY);
        map.put("year", year);
        int numM = time.indexOf("/", numY + 1);
        String month = time.substring(numY + 1, numM);
        int numD = time.indexOf("/", numM + 1);
        String day = time.substring(numM + 1, numM + 3);
        map.put("month", month);
        map.put("day", day);

        int numH = time.indexOf(":");
        String hour = time.substring(numH - 2, numH);

        int numMin = time.indexOf(":", numH + 1);
        String minute = time.substring(numH + 1, numMin);

        String second = time.substring(numMin + 1, numMin + 3);
        map.put("hour", hour);
        map.put("minute", minute);
        map.put("second", second);

        return map;
    }


}

class MyTimer extends TimerTask {
    private int ID;

    @Override
    public void run() {
        System.out.println("-----开始token过期-----");
        updatTokenforUsers();
        updatTokenforUsersdata();
        Date dateNow = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String nowTimeStr = df.format(dateNow);
        System.out.println("过期成功，当前时间为" + nowTimeStr);
    }

    public int getID() {
        return this.ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }


    //1.getConnection 获取数据库连接的方法，返回一个Connection对象
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        final String URL = "jdbc:mysql://127.0.0.1:3306/yunduyunpian?characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
        final String username = "root";
        final String password = "asdfgh123456";
        Connection conn = DriverManager.getConnection(URL, "root", "asdfgh123456");
        if (conn == null) {
            System.out.println("创建连接失败");
            close(conn);
            return null;
        } else {
            System.out.println("创建连接成功");
            return conn;
        }

    }

    //2.getPrepareStatement  获取PrepareStatement接口方法
    public static PreparedStatement getPrepareStatement(String sql) throws SQLException, ClassNotFoundException {
        Connection conn = getConnection();
        PreparedStatement ppsm = (PreparedStatement) conn.prepareStatement(sql);
        return ppsm;
    }

    //3.关闭prepareStatement接口的方法
    public static void close(PreparedStatement pstmt) {
        if (pstmt != null) {                        //避免出现空指针异常
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    //4.关闭connection接口的方法
    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    //5.关闭resultSet接口的方法
    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    //6.更新Token
    public void updatTokenforUsersdata() {
        Connection conn = null;
        PreparedStatement ppst = null;
        String sql = "update usersdata set Token = ? where id = ?";
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);
            ppst.setString(1, "过期");
            ppst.setInt(2, ID);
            ppst.execute();
            System.out.println("token过期成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {

        }
    }

    public void updatTokenforUsers() {
        Connection conn = null;
        PreparedStatement ppst = null;
        String sql = "update users set Token = ? where id = ?";
        System.out.println(ID);
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);
            ppst.setString(1, "过期");
            ppst.setInt(2, ID);
            ppst.execute();
            System.out.println("token过期成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {

        }
    }
}
